import { getCookie } from '__assets/js/util'
const IS_LOCALSTORAGE = !!(window.Storage && window.localStorage && window.localStorage instanceof Storage);
const user = {
	state:{
		user:null,
		loginStatus: false
	},

	mutations:{
		SET_USER_INFO: (state,info) => {
			if(!info || info == 'undefined') return;
			state.user = info;
			info = JSON.stringify(info);
			if(IS_LOCALSTORAGE){
				localStorage.setItem('user',info)
			}
		},
		GET_USER_INFO: (state) => {
			if(IS_LOCALSTORAGE){
				state.user = JSON.parse(localStorage.getItem('user'))
			}
		},
		DEL_USER_INFO: (state) => {
			if(IS_LOCALSTORAGE){
				state.user = null;
				state.loginStatus = false;
				localStorage.removeItem('user')
			}
		},
		SET_LOGIN_STATUS: (state,status) => {
			state.loginStatus = status;
		},
		CHECK_LOGIN_STATUS: (state) => {
			if(IS_LOCALSTORAGE){
				let ui = JSON.parse(localStorage.getItem('user'))
				if(ui && ui.id){
					state.loginStatus = true;
					state.user = ui;
				} else {
					state.loginStatus = false;
				}
			}
		}
	},

	actions:{
		setUserInfo({ commit }, info){
			if(info){
				commit('SET_USER_INFO', info);
			}
		},
		getUserInfo({ commit }){
			commit('GET_USER_INFO');
		},
		delUserInfo({ commit }){
			commit('DEL_USER_INFO');
		},
		setLoginStatus({ commit }, status){
			commit('SET_LOGIN_STATUS', status);
		},
		checkLoginStatus({ commit }){
			commit('CHECK_LOGIN_STATUS');
		}
	}
}

export default user